'use strict';

/**
 * @name AchievementModelCtrl
 * @description flag为true的话，则为创建，data.type为0为研究方向，1为论文，2为专利，3为项目
 * @author ZYF
 */

angular.module('metalabOperationApp').controller('AchievementModelCtrl', ['$scope', '$stateParams', '$rootScope', '$state', 'params', 'MetaResources', 'MetaRemind', '$uibModalInstance', function($scope, $stateParams, $rootScope, $state, params, MetaResources, MetaRemind, $uibModalInstance) {
    $scope.data = {};
    $scope.model = {};
    $scope.backup = {};
    $scope.model.isArea = false;
    var suffix = "";
    switch (params.data.type) {
        case 0:
            suffix = "研究方向";
            $scope.model.isArea = true;
            break;
        case 1:
            suffix = "论文";
            $scope.data.tag = "论文";
            break;
        case 2:
            suffix = "专利";
            $scope.data.tag = "专利";
            break;
        case 3:
            suffix = "项目";
            $scope.data.tag = "项目";
            break;
        default:
            break;
    }
    //true的话为创建
    if (params.flag) {
        $scope.model.title = "创建" + suffix;
        $scope.model.positive = "创 建";
        $scope.model.negative = "清 空";
    } else {
        $scope.model.title = "编辑" + suffix;
        $scope.model.positive = "保 存";
        $scope.model.negative = "重 置";
        $scope.data = params.data.data;
        $scope.backup = angular.copy(params.data.data);
    }

    $scope.positive = function() {
        if (params.flag) {
            $scope.create();
        } else {
            $scope.save();
        }
    };

    $scope.create = function() {
        MetaResources.talent[params.data.type===0?'createArea':'createAchievement']({'id': $stateParams.id}, $scope.data,
            function() {
                MetaRemind.success("恭喜", "创建成功");
                $state.reload();
            },
            function(error) {
                MetaRemind.serverError(error);
            });
        $uibModalInstance.dismiss('cancel');
    };

    $scope.save = function() {
        MetaResources.talent[params.data.type===0?'saveArea':'saveAchievement']({
                'id': params.data.data.id
            }, $scope.data,
            function() {
                MetaRemind.success("恭喜", "保存成功");
                $state.reload();
            },
            function(error) {
                MetaRemind.serverError(error);
            });
        $uibModalInstance.dismiss('cancel');
    };

    $scope.sure = function() {
        MetaResources.talent[params.data.type===0?'deleteArea':'deleteAchievement']({
                'id': params.data.data.id
            }, $scope.data,
            function() {
                MetaRemind.success("恭喜", "删除成功");
                $state.reload();
            },
            function(error) {
                MetaRemind.serverError(error);
            });
        $uibModalInstance.dismiss('cancel');
    };

    $scope.negative = function() {
        $scope.data = angular.copy($scope.backup);
    };

    $scope.cancel = function() {
        $uibModalInstance.dismiss('cancel');
    };

}]);
